/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 ******************************************************************************/
package com.kapil.framework.reader;

import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang.exception.ExceptionUtils;

import com.kapil.framework.logger.ILogger;
import com.kapil.framework.logger.LogFactory;


/**
 * This class is responsible of reading messages from a data source. Data sources may vary in an application, they must
 * provide their own implementations.
 * 
 * @author Kapil Viren Ahuja
 * 
 */
public abstract class ConfigurationReader
{
    private final static ILogger         LOGGER = LogFactory.getInstance().getLogger(ConfigurationReader.class);
    protected transient XMLConfiguration _config;


    /**
     * Default class constructor. We do not want applications to create instances without specifying a data source, so
     * this has been made private
     */
    @SuppressWarnings("unused")
    private ConfigurationReader()
    {
    }


    /**
     * Initializes the name of the data source from which message strings need to be read.
     * 
     * @param dataSourceName Name of the data source.
     */
    protected ConfigurationReader(String configFileName)
    {
        try
        {
            _config = new XMLConfiguration(configFileName);
        }
        catch (ConfigurationException e)
        {
            LOGGER.error("Error initializing the Configuration reader, with the file [" + configFileName + "]. Please see below for details. \n"
                    + ExceptionUtils.getFullStackTrace(e));
        }
    }


    /**
     * Reads a boolean property from a data source, provided a string key to locate the property.
     * 
     * @param key Key to locate the property.
     * @return A boolean value.
     */
    public abstract boolean getBooleanProperty(String name);


    /**
     * Reads an integer property from a data source, provided a string key to locate the property.
     * 
     * @param key Key to locate the property.
     * @return An integer value.
     */
    public abstract int getIntegerProperty(String name);


    /**
     * <p>
     * Returns a list of property values sharing the same name.
     * </p>
     * 
     * @param name A {@link java.lang.String} containing property name.
     * @return A {@link java.util.List} of {@link java.lang.String} values containing all values available for the
     *         property.
     */
    public abstract List<String> getList(String name);


    /**
     * Reads a string message from a data source, provided a string key to locate the message.
     * 
     * @param key Key to locate the message.
     * @return A text string.
     */
    public abstract String getProperty(String name);
}
